﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using DeviceMaintainGIS.Model.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMaintainGIS.BLL.User
{
    /// <summary>
    /// 用户操作
    /// </summary>
    public class UserOperation
    {

        /// <summary>
        /// 用户登录验证 成功后返回登录用户的菜单操作权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ReturnVM<UserResponse> UserLogin(LoginRequest request)
        {
            ReturnVM<UserResponse> list = new ReturnVM<UserResponse>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                string Password = request.Password.Substring(0, request.Password.Length - 2);
                //获取当前登录用户信息
                tbUser tu = Entities.tbUser.Where(i => i.AccountName == request.UserName && i.Password == Password).FirstOrDefault();
                if (tu == null)
                {
                    list.Succeed = false;
                    list.Message = "用户名密码错误！";
                    list.ErrorCode = "100";
                    return list;
                }
                //获取当前用户的角色
                tbUserRole tur = Entities.tbUserRole.Where(i => i.UserId == tu.ID).FirstOrDefault();
                if (tu == null)
                {
                    list.Succeed = false;
                    list.Message = "用户名没有分配权限！";
                    list.ErrorCode = "200";
                    return list;
                }
                UserResponse ur = new UserResponse();
                ur.UserID = tu.ID;
                ur.AccountName = tu.RealName;
                ur.UserName = tu.RealName;
                ur.RoleId = tur.RoleId;
                //获取角色下的菜单信息
                //List<int?> MenuIDs = 
                var linqMenus = from a in Entities.tbMenu.Where(i => i.MenuType == 1 && i.ParentId == 0)
                                join f in Entities.tbRoleMenuButton.Where(i => i.RoleId == tur.RoleId) on a.Id equals f.MenuId
                                into tmpButton
                                let k = tmpButton.FirstOrDefault()
                                select new MenuResponse
                                {
                                    MenuId = a.Id,
                                    MenuName = a.Name,
                                    MenuPath = a.LinkAddress,
                                    RoleId = k == null ? -1 : k.RoleId
                                };
                List<MenuResponse> MenuList = linqMenus.ToList();

                List<MenuResponse> newMenuList = new List<MenuResponse>();
                //获取每个菜单下的按钮信息
                foreach (MenuResponse mr in MenuList)
                {
                    if (mr.RoleId != -1)
                    {
                        MenuResponse newMenuResponse = mr;
                        var linqSeedMenus = from a in Entities.tbMenu.Where(i => i.MenuType == 1 && i.ParentId == mr.MenuId)
                                            select new MenuResponse
                                            {
                                                MenuId = a.Id,
                                                MenuName = a.Name,
                                                MenuPath = a.LinkAddress,
                                            };
                        newMenuResponse.MenuSeedList = linqSeedMenus.ToList();
                        foreach (MenuResponse Seedmr in newMenuResponse.MenuSeedList)
                        {
                            var linqs = from a in Entities.tbRoleMenuButton.Where(i => i.MenuId == Seedmr.MenuId)
                                        join f in Entities.tbButton on a.ButtonId equals f.Id
                                        into tmpButton
                                        let k = tmpButton.FirstOrDefault()
                                        select new MenuButtonResponse
                                        {
                                            ButtonId = a.ButtonId,
                                            ButtonName = k.Name,
                                            MenuType = k.Code == "add" ? 1 : k.Code == "edit" ? 2 : k.Code == "delete" ? 3 : 0,
                                        };
                            Seedmr.MenuButtonList = linqs.ToList();
                        }
                        newMenuList.Add(newMenuResponse);
                    }

                }
                //获取当前用户待处理数量
                ur.PendingNumber = Entities.RepairRelation_Tab.Where(i => i.State == 0 && i.NextOperatorID == tu.ID).Count();
                list.Succeed = true;
                ur.MenuList = newMenuList;
                list.obj = ur;
            }

            return list;
        }

        /// <summary>
        /// 根据角色ID获取角色下包含的用户信息
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public ReturnListVM<UserModel> GetUserInfoByRoleID(int RoleID)
        {
            ReturnListVM<UserModel> list = new ReturnListVM<UserModel>();

            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                var linq = from a in Entities.tbUserRole.Where(i => i.RoleId == RoleID)
                           join f in Entities.tbUser on a.UserId equals f.ID
                           into tmpUser
                           let User = tmpUser.FirstOrDefault()
                           select new UserModel
                           {
                               UserID = a.UserId,
                               UserName = User == null ? "" : User.RealName
                           };
                list.List = linq.ToList();
            }
            return list;
        }


        /// <summary>
        /// 根据条件查询用户待处理的报修信息
        /// </summary>
        /// <returns></returns>
        public ReturnListVM<UserPendingRepairResponse> GetUserUserPendingRepair(UserPendingRepairSelectRequest request)
        {
            ReturnListVM<UserPendingRepairResponse> list = new ReturnListVM<UserPendingRepairResponse>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {
                //当前要操作的任务
                var NextOperatorlinq = from k in Entities.RepairRelation_Tab.Where(i => i.State == 0 && i.NextOperatorID == request.UserID)
                                       select new UserPendingRepairResponse
                                       {
                                           AddTime = k.AddTime,
                                           RelationGuid = k.RelationGuid,
                                           RepairId = k.RepairId,
                                           Step = k.Step,
                                           Operator=k.Operator,
                                           TableName = k.TableName
                                       };
                list.List = NextOperatorlinq.ToList();

            }
            return list;
        }


    }
}
